package hanelsoft.vn.dictionaylearn.control.custom;

import hanelsoft.vn.dictionaylearn.R;
import hanelsoft.vn.dictionaylearn.model.entity.InfoVocabularyEdit;

import java.util.ArrayList;

import android.app.Activity;
import android.content.SharedPreferences;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.RadioButton;
import android.widget.TextView;

public class CustomVocabularyEdit extends ArrayAdapter<InfoVocabularyEdit>{
	String Tag ="CustomVocabularyEdit";
	SharedPreferences share;
	Activity context;
	ArrayList<InfoVocabularyEdit> list;
	InfoVocabularyEdit info;
	public CustomVocabularyEdit(Activity context,ArrayList<InfoVocabularyEdit> objects) {
		super(context, R.layout.custom_vocabulary_row_edit, objects);
		this.context = context;
		this.list = objects;
	}
	class ViewHolder
	{
		TextView name;
		RadioButton rbDelete;
	}
	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		View rowView;;
		if(convertView == null)
		{
			LayoutInflater inflater = context.getLayoutInflater();
			rowView = inflater.inflate(R.layout.custom_vocabulary_row_edit, null);
			ViewHolder viewHolder = new ViewHolder();
			viewHolder.name = (TextView)rowView.findViewById(R.id.custom_vocabulary_row_edit);
			viewHolder.rbDelete = (RadioButton)rowView.findViewById(R.id.custom_vocabulary_row_rdb);
			rowView.setTag(viewHolder);
		}
		else
		{
			rowView = convertView;
		}
		share = context.getSharedPreferences("CheckEdit", 1);
		share.edit().putInt("PositionCheckItem"+position, -1).commit();
		final ViewHolder holder = (ViewHolder)rowView.getTag();
		info = list.get(position);
		holder.name.setText(info.getName());
		holder.rbDelete.setChecked(false);
		holder.rbDelete.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if(holder.rbDelete.isChecked())
				{
					share.edit().putInt("PositionCheckItem"+position, position).commit();
				}
				else
				{
					share.edit().remove("PositionCheckItem"+position).commit();
				}
			}
		});
		return rowView;
	}
}

